<?php
namespace Common\Model;
use Think\Model;
class NewsModel extends Model{
	private $_db = '';
	public function __construct()
	{
		$this->_db = M('news');
	}
	public function select($data = array(), $limit = 100) {

        $conditions = $data;
        $list = $this->_db->where($conditions)->order('news_id desc')->limit($limit)->select();
        return $list;
    }
	public function insert($data=[])
	{
		// var_dump($data);die;
		if(!is_array($data) || !$data){
			return 0;
		}
		// var_dump($data);die;
		$data['create_time'] = time();
		$data['username'] = getLoginUsername();
		unset($data['content']);
		// var_dump($data);die;
		$res = $this->_db->add($data);
		return $res;
	}
	// 分页
	public function getNews($data,$page,$pageSize=10)
	{
		$conditions = $data;
		if(isset($data['title']) && $data['title']){
			$conditions['catid'] = ['like','%'.$data['title'].'%'];
		}
		if(isset($data['catid']) && $data['catid']){
			$conditions['catid'] = intval($data['catid']);
		}
		$offset = ($page - 1) * $pageSize;
		$list = $this->_db->where($conditions)->order('listorder desc, news_id desc')->limit($offset,$pageSize)->select();
		// var_dump($offset);die;
		return $list;
	}
	// 获取总数
	public function getNewsCount($data = [])
	{
		$conditions = $data;
		if(isset($data['title']) && $data['title']){
			$conditions['catid'] = ['like','%'.$data['title'].'%'];
		}
		if(isset($data['catid']) && $data['catid']){
			$conditions['catid'] = intval($data['catid']);
		}
		return $this->_db->where($conditions)->count();
	}
	public function find($id)
	{
		$data = $this->_db->where('news_id='.$id)->find();
		return $data;
	}
	public function updateById($id,$data)
	{
		if(!$id || !is_numeric($id)){
			throw_exception('ID不合法');
		} 
		if(!$data || !id_array($data)){
			throw_exception('更新数据不合法');
		}
		return $this->_db->where('news_id='.$id)->save($data);
	}
	public function updateStatusById($id,$status)
	{
		if(!is_numeric($status)){
			throw_exception('status不能为非数字');
		}
		if(!$id || !is_numeric($id)){
			throw_exception('id不合法');
		}
		$data['status'] = $status;
		return $this->_db->where('news_id='.$id)->save($data);
	}
	public function updateNewsListorderById($id,$listorder)
	{
		if(!$id || !is_numeric($id)){
			throw_exception('ID不合法');
		}
		$data = ['listorder'=>intval($listorder)];
		return $this->_db->where('news_id='.$id)->save($data);
	}
	// 查询所选ID的信息
	public function getNewsByNewsIdIn($newsIds)
	{
		if(!is_array($newsIds)){
			throw_exception('参数不合法');
		}
		$data = [
			'news_id'=>['in',implode(',',$newsIds)],
		];
		return $this->_db->where($data)->select();
	}
	// 获取排行的数据
	public function getRank($data = array(), $limit = 100) {
        $list = $this->_db->where($data)->order('news_id desc ')->limit($limit)->select();
        return $list;
    }
    public function updateCount($id, $count) {
        if(!$id || !is_numeric($id)) {
            throw_exception("ID 不合法");

        }
        if(!is_numeric($count)) {
            throw_exception("count不能为非数字");
        }

        $data['count'] = $count;
       	return $this->_db->where('news_id='.$id)->save($data);

    }
    // 最大阅读数操作
    public function maxcount() {
        $data = array(
            'status' => 1,
        );
        return $this->_db->where($data)->order('count desc')->limit(1)->find();
    }
}